/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _CLIENT_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "client.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
int16s main(int16s argc, int8u *argv[])
{
	int16u	i = 0;
	int16s 	sockfd;
	int16u 	len,lenge;
	struct 	sockaddr_in address;
	int16s 	result;
	int16u	answer=0;;
	int8u 	*p,*buf;
	p = argv[1];

	if(argc == 2)
	{
		for(i=0;argv[1][i];i++)
		{
		if((argv[1][i]<'A')||((argv[1][i]>'Z')&&(argv[1][i]<'a'))||(argv[1][i]>'z'))
		{
			printf("Error, the charactor must be 'A'-'Z' or 'a'-'z'\n");
			return 0;
		}
		}
		// Create a socket for the client. 
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		// Socket name, as agreed with the server.
		address.sin_family = AF_INET;
		address.sin_addr.s_addr = inet_addr("192.168.1.245");
		address.sin_port = 6;
		len = sizeof(address);

		// Connect our socket to the server's socket.
		result = connect(sockfd, (struct sockaddr *)&address, len);
		if(result == -1)
		{
			perror("OOPS!!: connect function error");
			exit(1);
		}

		// Write memory to server 
		lenge = strlenge(argv[1]);
		write(sockfd, &lenge, sizeof(int16u));		// 1
		read(sockfd, &answer, sizeof(int16u));		// 2
		if(answer)
		{
			// Write string
			write(sockfd, p,lenge*sizeof(int8u));		// 3
			// Wait for reply
			read(sockfd, &answer, sizeof(int16u));		//4
			if(answer)
			{
				
				if(answer == 2) // String not match
				{
					printf("String not match\n");
					answer = 0;
					write(sockfd, &answer, sizeof(int16u));		//5
					return(0);
				}
				else if(answer == 1) // Complete
				{
					buf = (int8u*)malloc(lenge*sizeof(int8u));
  			        write(sockfd, &answer, sizeof(int16u));		//5
					read(sockfd,buf,lenge*sizeof(int8u));		//6
					printf("%s\n",buf);
					free(buf);
				}
				
			}
			else
			{
				printf("Server not reply about string\n");
			}
		}
		else
		{
			printf("Server not reply about memory\n");
		}

		// Close socket and exit program.
		close(sockfd);
		exit(0);
	}
	else
	{
		printf("Error,the number of argument must be 2\n");
	}
	return(0);
}

/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/
 
